package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.core.page.TableDataInfo;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.product.domain.ProductFile;
import com.xingkong.product.service.ProductFileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品文件Controller
 *
 * @author yiyue
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/product/file")
public class ProductFileController extends BaseController {
    private String prefix = "product/file";

    @Autowired
    private ProductFileService productFileService;

    @RequiresPermissions("product:file:view")
    @GetMapping()
    public String file() {
        return prefix + "/file";
    }

    /**
     * 查询商品文件列表
     */
    @RequiresPermissions("product:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductFile productFile) {
        startPage();
        return getDataTable(productFileService.list(new QueryWrapper<ProductFile>(productFile)));
    }

    /**
     * 导出商品文件列表
     */
    @RequiresPermissions("product:file:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductFile productFile) {
        List<ProductFile> list = productFileService.list(new QueryWrapper<>());
        ExcelUtil<ProductFile> util = new ExcelUtil<ProductFile>(ProductFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 新增商品文件
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品文件
     */
    @RequiresPermissions("product:file:add")
    @Log(title = "商品文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductFile productFile) {
        return toAjax(productFileService.save(productFile));
    }

    /**
     * 修改商品文件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductFile productFile = productFileService.getById(id);
        mmap.put("productFile", productFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品文件
     */
    @RequiresPermissions("product:file:edit")
    @Log(title = "商品文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductFile productFile) {
        return toAjax(productFileService.updateById(productFile));
    }

    /**
     * 删除商品文件
     */
    @RequiresPermissions("product:file:remove")
    @Log(title = "商品文件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productFileService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
